﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MLSMooc.Helper
{
    public static class EncryptHelper
    {
        private static readonly string _key = "YANCEY1Z2W3R4C5G6A7G8B9C0LYANCEY";

        /// <summary>
        /// string的加密操作，秘钥对外不可见
        /// </summary>
        /// <param name="enStr"></param>
        /// <returns></returns>
        public static string ToAESEncrypt(this string enStr)
        {
            if (string.IsNullOrEmpty(enStr))
            {
                return string.Empty;
            }

            //拆解
            byte[] strArray = Encoding.UTF8.GetBytes(enStr);
            //初始化加密类
            RijndaelManaged rm = new RijndaelManaged()
            {
                Key = Encoding.UTF8.GetBytes(_key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            //加密
            ICryptoTransform enTransform = rm.CreateEncryptor();
            Byte[] resultArray = enTransform.TransformFinalBlock(strArray, 0, strArray.Length);
            //返回string
            return Convert.ToBase64String(resultArray);
        }

        /// <summary>
        /// string的解密操作，秘钥对外不可见
        /// </summary>
        /// <param name="deStr"></param>
        /// <returns></returns>
        public static string ToAESDecrypt(this string deStr)
        {
            if (string.IsNullOrEmpty(deStr))
            {
                return string.Empty;
            }
            //拆解
            byte[] strArray = Convert.FromBase64String(deStr);
            //初始化加密类
            RijndaelManaged rm = new RijndaelManaged()
            {
                Key = Encoding.UTF8.GetBytes(_key),
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };
            //解密
            ICryptoTransform deTransform = rm.CreateDecryptor();
            Byte[] resultArray = deTransform.TransformFinalBlock(strArray, 0, strArray.Length);
            //返回string
            return Encoding.UTF8.GetString(resultArray);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string ToMD5(this string str)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] fromByte = Encoding.Unicode.GetBytes(str);
            byte[] targetByte = md5.ComputeHash(fromByte);

            string result = string.Empty;
            for (int i = 0; i < targetByte.Length; i++)
            {
                result += targetByte[i].ToString("x").PadLeft(2, '0');
            }
            return result.ToUpper();
        }

        /// <summary>
        /// 生成一对公钥私钥
        /// </summary>
        /// <returns></returns>
        public static KeyValuePair<string, string> GetRSAPair()
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            string publicKey = Convert.ToBase64String(rsa.ExportCspBlob(false));    //公钥
            string privateKey = Convert.ToBase64String(rsa.ExportCspBlob(true));    //私钥
            return new KeyValuePair<string, string>(publicKey, privateKey);
        }

        /// <summary>
        /// 加密需要发送的数据
        /// </summary>
        /// <param name="publicKey">对方的公钥</param>
        /// <param name="enStr">需要加密的内容</param>
        /// <returns>base64编码的字符串</returns>
        public static string ToRSAEncrypt(string publicKey, string enStr)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(publicKey);
            byte[] bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(enStr), false);
            return Convert.ToBase64String(bytes);
        }

        /// <summary>
        /// 解密base64编码的字符串
        /// </summary>
        /// <param name="privateKey">自己的私钥</param>
        /// <param name="deStr">需要解密的内容</param>
        /// <returns></returns>
        public static string ToRSADncrypt(string privateKey, string deStr)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            rsa.FromXmlString(privateKey);
            byte[] bytes = rsa.Decrypt(Convert.FromBase64String(deStr), false);
            return Encoding.UTF8.GetString(bytes);
        }
    }
}
